// app_admin//goods-cat/goods-cat.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    menu_id: null,
    open_index: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getCat();
  },
  /**
   * 商品分类
   */
  getCat: function(){
    var that = this;
    getApp().core.showLoading({
      content: "加载中",
  }), getApp().request({
      url: getApp().api.mch.cat.get_list,
      method: "get",
      success: function(e) {
        getApp().core.hideLoading()
        if (0 == e.code) {
          that.setData({
            list: e.data.list,
          });
        }
      },
      fail: function(t) {
        getApp().core.showToast({
          title: t.msg,
          icon: 'none'
        });
      }
  });
  },

  onOpen: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      open_index: index == this.data.open_index ? null : index
    })
  },
  onMenu: function(t) {
    var id = t.currentTarget.dataset.id;
    this.setData({
      menu_id: id == this.data.menu_id ? null : id
    })
  },
  // 编辑
  toEdit: function(t) {
    var a = t.currentTarget.dataset.id;
    getApp().core.navigateTo({
      url: '/mch/m/add-cat/add-cat?id=' + a
    });
  },
  toDelete: function(t) {
    var that = this;
    var a = t.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除？',
      confirmText: '确定',
      success (res) {
        if (res.confirm) {
          getApp().core.showLoading(),
          getApp().request({
            url: getApp().api.mch.cat.delete,
            data: {
              id: a,
            },
            method: "post",
            success: function(e) {
              getApp().core.hideLoading()
              if (0 == e.code) {
                that.getCat();
              } else getApp().core.showToast({
                title: e.msg,
                icon: 'none'
              });
            },
            fail: function(t) {
              getApp().core.showToast({
                title: t.msg,
                icon: 'none'
              });
            }
          });
        }
      }
    })
  },

  toScreen: function (t) {
    var that = this;
    var id = t.currentTarget.dataset.id;
    var screen_price = t.currentTarget.dataset.screen_price;
    var data = {};
    data.mch_cat_id = id
    data.screen_price = screen_price
    data.pay_type = 'WECHAT_PAY'

    wx.showModal({
      title: '提示',
      content: '同业屏蔽需要'+screen_price+'元，是否购买同业屏蔽?',
      confirmText: '确定',
      success (res) {
        if (res.confirm) {
          getApp().core.showLoading({
            title: "提交中"
          }), 
          getApp().request({
            url: getApp().api.mch.cat.submit,
            data: data,
            method: "POST",
            success: function(e) {
      
              if ( 0 == e.code) {
                getApp().page.bindParent({
                  parent_id: getApp().core.getStorageSync(getApp().const.PARENT_ID),
                  condition: 1
                })
               
                return setTimeout(function() {
                      getApp().core.hideLoading();
                  }, 1e3), setOnShowScene("pay"), void getApp().core.requestPayment({
                      _res: e,
                      timeStamp: e.data.timeStamp,
                      nonceStr: e.data.nonceStr,
                      package: e.data.package,
                      signType: e.data.signType,
                      paySign: e.data.paySign,
                      success: function(e) {},
                      fail: function(e) {},
                      complete: function(e) {
      
                          "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? (getApp().page.bindParent({
                            parent_id: getApp().core.getStorageSync(getApp().const.PARENT_ID),
                            condition: 2
                        }), getApp().core.showModal({
                            title: "提示",
                            content: "购买成功",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(e) {
                                e.confirm && getApp().core.navigateBack({
                                    delta: 1
                                });
                            }
                        })) : getApp().core.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认"
                        });
                      
                      }
                  });
                 
              } else {
                getApp().core.showModal({
                  title: "提示",
                  content: e.msg,
                  showCancel: !1
                })
              }
           
              that.getCat();
              getApp().core.hideLoading();
            }
          })
        }
      }
    })
   
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})